
import is from './is'

var regulars = {
    year: {
        regExp: "^(19|20)\\d{2}$",
        message: "格式错误"
    },
    number: {
        regExp: "^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$",
        message: "只能为数字"
    },
    bankNum: {
        regExp: "^(\\d{16}|\\d{19})$",
        message: "格式错误"
    },
    telephone: {
        regExp: "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$",
        message: "格式错误"
    },
    int: {
        regExp: "^[0-9]*$",
        message: "只能为正整数"
    },
    phone: {
        regExp: "^[1][0-9]{10}$",
        message: "格式错误"
    },
    allChinese: {
        regExp: "^([\\u4E00-\\u9FA5]+，?)+$",
        message: "只能为中文"
    },
    haveChinese: {
        regExp: "[\\u4E00-\\u9FFF]+",
        message: "中含有汉字"
    },
    idCard: {
        regExp: ["^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$", "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$"],
        message: "格式错误",
        event: (regExp, str) => {
            var re1 = new RegExp(regExp[0]);
            var re2 = new RegExp(regExp[1]);
            return re1.test(str) || re2.test(str) ? false : true;
        }
    },
    url: {
        regExp: "^((https|http|ftp|rtsp|mms)?:\/\/)[^\\s]+",
        message: "格式错误"
    },
    email: {
        regExp: "^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\\.)+[A-Za-z0-9]{2,3}$",
        message: "格式错误"
    },
    Special: {
        exclude: ["~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "{", "}", "[", "]", "(", ")", ":", ";", "'", "|", "\\", "<", ">", "?", "/", "<<", ">>", "||", "//", "administrators", "administrator", "管理员", "系统管理员", "admin", "select", "delete", "update", "insert", "create", "drop", "alter", "trancate"],
        message: "不能包含特殊字符"
    },
    required: {
        regExp: "^[ ]+$",
        message: "不能为空",
        event: (regExp, str) => {
            if (str == "" || str == undefined || str == null || str == NaN) return !false;
            return new RegExp(regExp).test(str);
        }
    },
};

function Check(val, Obj) {
    if (Obj.event) {
        return Obj.event(Obj.regExp, val)
    } else if (Obj.regExp) {
        return !new RegExp(Obj.regExp).test(val)
    };
    if (Obj.exclude) {
        val = val.toLowerCase();
        for (var i = 0; i < Obj.exclude.length; i++) {
            if (val.indexOf(Obj.exclude[i]) >= 0) {
                return true;
            }
        }
        return false;
    };
    return false;
}

function TypeChange(key, value) {
    if (is(key, 'string')) {
        var regularsItem = regulars[key];
        var ans = Check(value, regularsItem);
        if (ans) {
            return regularsItem.message;
        } else {
            return null;
        }
    } else if (is(key, 'object')) {
        var ans = Check(value, key);
        if (ans) {
            return key.message;
        } else {
            return null;
        }
    }
}

function rules(rules, value, label) {
    if (is(rules, 'array')) {
        var result = false;
        for (var i = 0; i < rules.length; i++) {
            var element = rules[i];
            var ans = TypeChange(element, value);
            if (ans) {
                result = ans;
                break;
            };
        };
    } else {
        var result = TypeChange(rules, value);
    }
    if (result) {
        return (label ? label : '') + result;
    } else {
        return null;
    };
}


export default rules